package com.sinkingsquare.dao.hibernate;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.sinkingsquare.dao.AccountDao;
import com.sinkingsquare.domain.Account;

@Component("accountDao")
public class AccountDaoImpl implements AccountDao {
	
	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@SuppressWarnings("unchecked")
	public boolean checkAccountExistsWithName(String username) {	
		List<Account> users = hibernateTemplate.find("from Account a where a.username = '" + username + "'");
		if (users != null && users.size() > 0) {
			return true;
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public List<Account> getAllAcounts() {
		return (List<Account>)this.hibernateTemplate.find("from User");
	}

	public Account loadAccountById(int id) {
		return (Account)this.hibernateTemplate.load(Account.class, id);
	}

	public void save(Account account) {
		hibernateTemplate.save(account);	
	}

	@SuppressWarnings("unchecked")
	public boolean checkAccountExistsWithNameAndPwd(String username,
			String password) {
		List<Account> account = hibernateTemplate.find("from Account a where a.username=? and password=?", new Object[]{username, password});
		if(account.size()>0)
			return true;
		
		return false;
	}

}
